package org.momo.rpc.ratelimit;

import org.momo.rpc.annotion.RateLimit;
import org.momo.rpc.ratelimit.impl.FixedRateLimiter;
import org.momo.rpc.ratelimit.impl.SlidingWindowRateLimiter;
import org.momo.rpc.ratelimit.impl.TokenBucketRateLimiter;

public class RateLimitProvider {


    public static RateLimiter getRateLimiter(RateLimit rateLimit) {
        switch (rateLimit.type()) {
            case FIXED:
                return new FixedRateLimiter(rateLimit.interval(), rateLimit.permitPerInterval());
            case SLIDING:
                return new SlidingWindowRateLimiter(rateLimit.interval(), rateLimit.permitPerInterval());
            case TOKEN:
                return new TokenBucketRateLimiter(rateLimit.capacity(), rateLimit.rate());
        }
        return null;
    }

}
